package com.ayu.chatroom.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author 徐林玉
 * @Description WebSocketServer
 * @Date 2022/10/28 11:19
 * @Version 1.0
 */

@Component
@Slf4j
public class WebSocketServer {



    private String hostIp="121.229.103.207";
//        private String hostIp="192.168.2.20";

    private static class SingletonWSServer{
        static final WebSocketServer instance = new WebSocketServer();
    }

    public static WebSocketServer getInstance(){

        return SingletonWSServer.instance;
    }


    private EventLoopGroup mainGroup;

    private EventLoopGroup subGroup;

    private ServerBootstrap server;

    private ChannelFuture future;

    public WebSocketServer(){

        mainGroup = new NioEventLoopGroup();
        subGroup = new NioEventLoopGroup();
        server = new ServerBootstrap();
        server.group(mainGroup,subGroup).channel(NioServerSocketChannel.class)
                .childHandler(new WSServerInitialzer());

    }

    public void start(){
        log.info(hostIp);
        this.future = server.bind(hostIp,9999);
        if(future.isSuccess()){

            System.out.println("启动 Netty服务器 成功");
        }
    }


}
